package com.lz.ttapi;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.FilterWord;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTBannerAd;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.fyhdxxl.mz.R;
import com.lz.dialog.DislikeDialog;
import com.lz.uikit.Constants;

import org.cocos2dx.javascript.AppActivity;
import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;

import java.util.List;

public class TTGameBannerAd {
    public static final String TAG = "TTGameBannerAd";
    public static  AppActivity activity ;
    private static TTAdNative mTTAdNative;
    private static FrameLayout mBannerContainer;
    private static TTNativeExpressAd mTTAd;
    private static boolean mHasShowDownloadActive = false;
    private static boolean isLoadedState = false; //加载广告状态
    private static boolean isAdShow = false;

    public static void init(Context content){
        activity = (AppActivity)content;
        //step2:创建TTAdNative对象，createAdNative(Context context) banner广告context需要传入Activity对象
        mTTAdNative = TTAdManagerHolder.get().createAdNative(content);
        //step3:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        TTAdManagerHolder.get().requestPermissionIfNecessary(content);
        mBannerContainer = (FrameLayout) activity.findViewById(R.id.banner_container);
        loadBannerAd();
    }

    public static void loadBannerAd() {
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(Constants.TT_BANNER_ID) //广告位id
                .setSupportDeepLink(true)
                .setImageAcceptedSize(400,120)
                .build();

        //step5:请求广告，对请求回调的广告作渲染处理
        mTTAdNative.loadBannerAd(adSlot, new TTAdNative.BannerAdListener() {
            @Override
            public void onError(int code, String message) {
                System.out.println("Banner==>err"+message.toString());
                mBannerContainer.removeAllViews();
            }
            @Override
            public void onBannerAdLoad(final TTBannerAd ad) {
                if (ad == null) {
                    System.out.println("Banner==>空");
                    return;
                }
                View bannerView = ad.getBannerView();
                if (bannerView == null) {
                    System.out.println("Banner==>空2");
                    return;
                }
                ad.setSlideIntervalTime(30 * 1000);
                mBannerContainer.removeAllViews();
                mBannerContainer.addView(bannerView);

                //设置广告互动监听回调
                ad.setBannerInteractionListener(new TTBannerAd.AdInteractionListener() {
                    @Override
                    public void onAdClicked(View view, int type) {

                    }

                    @Override
                    public void onAdShow(View view, int type) {

                    }
                });


                bindDownloadListener(ad);

                ad.setShowDislikeIcon(new TTAdDislike.DislikeInteractionCallback() {
                    @Override
                    public void onSelected(int position, String value) {
                       // TToast.show(mContext, "点击 " + value);
                        //用户选择不喜欢原因后，移除广告展示
                        mBannerContainer.removeAllViews();
                    }
                    @Override
                    public void onCancel() {
                        //TToast.show(mContext, "点击取消 ");
                    }
                });
            }
        });
    }

    private static void bindDownloadListener(TTBannerAd ad) {
        ad.setDownloadListener(new TTAppDownloadListener() {
            @Override
            public void onIdle() {
               // TToast.show(BannerActivity.this, "点击图片开始下载", Toast.LENGTH_LONG);
            }

            @Override
            public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                if (!mHasShowDownloadActive) {
                    mHasShowDownloadActive = true;
                    // TToast.show(BannerActivity.this, "下载中，点击图片暂停", Toast.LENGTH_LONG);
                }
            }

            @Override
            public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                //TToast.show(BannerActivity.this, "下载暂停，点击图片继续", Toast.LENGTH_LONG);
            }

            @Override
            public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                // TToast.show(BannerActivity.this, "下载失败，点击图片重新下载", Toast.LENGTH_LONG);
            }

            @Override
            public void onInstalled(String fileName, String appName) {
                //TToast.show(BannerActivity.this, "安装完成，点击图片打开", Toast.LENGTH_LONG);
            }

            @Override
            public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                //TToast.show(BannerActivity.this, "点击图片安装", Toast.LENGTH_LONG);
            }
        });
    }

    //装载成功
    public static boolean getAdLoaded(){
        return  isLoadedState ;
    }

    public static void onDestroyExpress() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mBannerContainer.setVisibility(View.GONE);
            }
        });
    }
    public static void onDestroy() {
        if (mTTAd != null) {
            mTTAd.destroy();
        }
    }

    public static void showBanner() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mBannerContainer.setVisibility(View.VISIBLE);
            }
        });
    }

    public static void hideBanner() {
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mBannerContainer.setVisibility(View.GONE);
            }
        });
    }

    public static void switchBanner() {
        loadBannerAd();
    }


}
